unit ICQ_RemoteCP_Store;

interface

uses
 StringsSettings,
 xIniFile;

var
 Sets: TStringsSettings = nil;
 SetMain: TxIniFile = nil;
 SetUIN: TxIniFile = nil;
 IniFile: String = '';
 UINIniFile: String = '';
 IniKey: String = '';
 UINIniKey: String = '';
 LogFilename: String = '';

procedure SaveMain;
procedure SaveUser;
//
function GetUINSetting(UIN, Key: String): String;
procedure SetUINSetting(UIN, Key, Value: String; var Flag: Boolean);
function GetUINListFormat(UIN: String; Mode: Integer): String;
procedure SetUINListFormat(UIN: String; Mode: Integer; Value: String; var Flag: Boolean);
function GetUnits(UIN: String; Speed: Boolean = false): Integer;
procedure SetUnits(UIN: String; Value: Integer; Speed: Boolean = false);

implementation

uses
 SysUtils,
 StrUtils,
 DMPluginConsts,
 ICQ_RemoteCP_Settings;

procedure SaveMain;
begin
 if Assigned(SetMain) then
  begin
   case Sets[cSetStore] of
    xitReg:
      Sets.SaveValuesToReg(SetMain.Reg, IniKey);
    else
     begin
      Sets.SaveValuesToIni(SetMain.Ini);
      SetMain.UpdateFile;
     end;
    end;
  end;
end;

procedure SaveUser;
begin
 if Assigned(SetUIN) then
   SetUIN.UpdateFile;
end;

function GetUINSetting(UIN, Key: String): String;
begin
 Result:='';
 if Assigned(SetUIN) then
  begin
   if SetUIN.SectionExists(UIN) then
    begin
     if SetUIN.ValueExists(UIN, Key) then
       Result:=SetUIN.ReadString(UIN, Key, '');
    end;
  end;
end;

procedure SetUINSetting(UIN, Key, Value: String; var Flag: Boolean);
begin
 if Assigned(SetUIN) then
  begin
   if Value='' then
    begin
     if SetUIN.ValueExists(UIN, Key) then
       SetUIN.DeleteKey(UIN, Key);
    end
   else
     SetUIN.WriteString(UIN, Key, Value);
   if Sets[cSetDumpUser]=cTimeEvery then
     SetUIN.UpdateFile;
   Flag:=true;
  end;
end;

function GetUINListFormat(UIN: String; Mode: Integer): String;
begin
 if Assigned(SetUIN) then
  begin
   case Mode of
    dsPause: Result:=SetUIN.ReadString(UIN, cFormatP, cDefaultFormat);
    dsDownloaded: Result:=SetUIN.ReadString(UIN, cFormatC, cDefaultFormat);
    dsDownloading: Result:=SetUIN.ReadString(UIN, cFormatD, cDefaultFormat);
    dsError: Result:=SetUIN.ReadString(UIN, cFormatE, cDefaultFormat);
    dsQueue: Result:=SetUIN.ReadString(UIN, cFormatQ, cDefaultFormat);
    dsAll: Result:=SetUIN.ReadString(UIN, cFormatA, cDefaultFormat);
    else
      Result:='';
    end;
  end
 else
   Result:='';
end;

procedure SetUINListFormat(UIN: String; Mode: Integer; Value: String; var Flag: Boolean);
 var
  Erase: Boolean;
begin
 if Assigned(SetUIN) then
  begin
   Erase:=Value='';
   case Mode of
    dsPause:
     begin
      if Erase then
       begin
        if SetUIN.ValueExists(UIN, cFormatP) then
          SetUIN.DeleteKey(UIN, cFormatP);
       end
      else
        SetUIN.WriteString(UIN, cFormatP, Value);
     end;
    dsDownloaded:
     begin
      if Erase then
       begin
        if SetUIN.ValueExists(UIN, cFormatC) then
          SetUIN.DeleteKey(UIN, cFormatC);
       end
      else
        SetUIN.WriteString(UIN, cFormatC, Value);
     end;
    dsDownloading:
     begin
      if Erase then
       begin
        if SetUIN.ValueExists(UIN, cFormatD) then
          SetUIN.DeleteKey(UIN, cFormatD);
       end
      else
        SetUIN.WriteString(UIN, cFormatD, Value);
     end;
    dsError:
     begin
      if Erase then
       begin
        if SetUIN.ValueExists(UIN, cFormatE) then
          SetUIN.DeleteKey(UIN, cFormatE);
       end
      else
        SetUIN.WriteString(UIN, cFormatE, Value);
     end;
    dsQueue:
     begin
      if Erase then
       begin
        if SetUIN.ValueExists(UIN, cFormatQ) then
          SetUIN.DeleteKey(UIN, cFormatQ);
       end
      else
        SetUIN.WriteString(UIN, cFormatQ, Value);
     end;
    dsAll:
     begin
      if Erase then
       begin
        if SetUIN.ValueExists(UIN, cFormatA) then
          SetUIN.DeleteKey(UIN, cFormatA);
       end
      else
        SetUIN.WriteString(UIN, cFormatA, Value);
     end;
    end;
   if Sets[cSetDumpUser]=cTimeEvery then
     SetUIN.UpdateFile;
   Flag:=true;
  end;
end;

function GetUnits(UIN: String; Speed: Boolean = false): Integer;
begin
 if Assigned(SetUIN) then
  begin
   Result:=StrToIntDef(SetUIN.ReadString(UIN, IfThen(Speed, cUnitsSpeed, cUnits), ''), -1);
   if Result<0 then
     Result:=cDefaultUnits
   else
    begin
     if Result>=cUnitsLen then
       Result:=0;
    end;
  end
 else
   Result:=0;
end;

procedure SetUnits(UIN: String; Value: Integer; Speed: Boolean = false);
begin
 if Assigned(SetUIN) then
  begin
   if Value in [0..cUnitsLen-1] then
    begin
     SetUIN.WriteString(UIN, IfThen(Speed, cUnitsSpeed, cUnits), IntToStr(Value));
     if Sets[cSetDumpUser]=cTimeEvery then
       SetUIN.UpdateFile;
    end;
  end;
end;

end.
 